﻿using UnityEngine;
using UnityEngine.UI;
using System;

public class TextAlphaChange : MonoBehaviour
{
    private float dir = 1.0f;
    private float alphaChgSpeed = 0.8f;
    private float leastAlpha = 0.3f;
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Color c = GetComponent<Text>().color;
        float a = c.a + dir * Time.deltaTime * alphaChgSpeed;
        if (a > 1.0f || a < leastAlpha)
        {
            dir *= -1.0f; // 反向变化透明度
        }
        // 重新计算透明度
        a = (c.a + dir * Time.deltaTime * alphaChgSpeed) % 1.0f;
        c.a = Mathf.Max(a, leastAlpha); // 透明度至少是30%
        GetComponent<Text>().color = c;
    }
}
